<?php
namespace Common\Model;

use Think\Model;


class NavigationModel extends Model
{

    public $_validate = array();

    public function addNav()
    {
        $this->_validate = array(
            array('name', 'require', '菜单名称不能为空'),
            array('url', 'require', '菜单链接地址不能为空'),
        );
        if (!$this->create()) {
            return false;
        }
        $data = $_POST;
        $this->add($data);
        return true;
    }

    public function getList(){
        $p = I('p',0,'int');
        $limit = C('DEFALUT_PAGE_SIZE');
        $startNum = $p > 0 ? ($p - 1) * $limit : 0;
        $count = $this->field("count(*) as count")->find();
        $data = $this->limit($startNum,$limit)->select();
        return array(
            'count' => $count['count'],
            'data'  => $data
        );
    }

    public function getMenuList(){
        $menu = $this->order('sort asc')->select();
        //实例化扩展类 栏目树
        $list = new \Org\Util\Data;
        $menuList = $list::tree($menu, 'name', 'id', 'pid');
        return $menuList;
    }


    public function editNav()
    {
        $this->_validate = array(
            array('name', 'require', '菜单名称不能为空'),
            array('url', 'require', '菜单链接地址不能为空'),
        );
        if (!$this->create()) {
            return false;
        }

        $data = $_POST;
        $data['id'] = I('get.id');
        $this->save($data);
        return true;

    }

}
